package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.AwardprojectMapper;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.CheckRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 奖项项目服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class AwardprojectServiceImpl extends ServiceImpl<AwardprojectMapper, Awardproject> implements IAwardprojectService {

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    private IAwardService iAwardService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 添加项目
     * @param awardproject 要添加的奖项项目
     * @return 新增操作是否成功
     */
    @Override
    public boolean addAwardProject(Awardproject awardproject) {
        // 拦截普通用户在非法状态修改的操作
        Award award = iAwardService.getOne(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getAwardID, awardproject.getAwardID()));
        checkRoleUtil.checkPermissionAchievement(award.getStatus());

        if (StrUtil.isEmpty(awardproject.getProjectname()) || StrUtil.isBlank(awardproject.getProjectname())) {
            throw new BaseException("请输入项目名称!");
        }

        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectName, awardproject.getProjectname()));

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectName, awardproject.getProjectname()));

        if (Objects.nonNull(porproject)) {
            awardproject.setHead(porproject.getHead());
            awardproject.setProjectID(porproject.getProjectId());
            if (Objects.isNull(awardproject.getStartTime())) {
                awardproject.setStartTime(porproject.getStartTime());
            }
            if (Objects.isNull(awardproject.getEndTime())) {
                awardproject.setEndTime(porproject.getEndTime());
            }
        } else if (Objects.nonNull(tranproject)) {
            awardproject.setHead(tranproject.getHead());
            awardproject.setProjectID(tranproject.getProjectId());
            if (Objects.isNull(awardproject.getStartTime())) {
                awardproject.setStartTime(tranproject.getStartTime());
            }
            if (Objects.isNull(awardproject.getEndTime())) {
                awardproject.setEndTime(tranproject.getEndTime());
            }
        } else {
            throw new BaseException("项目不存在!");
        }

        // 判断该项目是否已经添加过在该奖项中
        List<Awardproject> awardprojectList = query().eq("projectName", awardproject.getProjectname())
                .eq("AwardID", awardproject.getAwardID()).list();
        if (!awardprojectList.isEmpty()) {
            throw new BaseException("该项目已经添加过在该奖项中!");
        }

        return baseMapper.insert(awardproject) == 1;
    }

    /**
     * 删除奖项项目
     * @param awardProjectID 奖项项目编号
     * @return 删除结果
     */
    @Override
    public boolean removeAwardProject(String awardProjectID) {
        Awardproject awardproject = super.getOne(Wrappers.lambdaQuery(Awardproject.class)
                .eq(Awardproject::getAwardProjectID, awardProjectID));
        if (Objects.isNull(awardproject)) {
            throw new BaseException("不存在该关联项目");
        }

        // 拦截普通用户在非法状态修改的操作
        Award award = iAwardService.getOne(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getAwardID, awardproject.getAwardID()));
        checkRoleUtil.checkPermissionAchievement(award.getStatus());

        return baseMapper.deleteById(awardproject) == 1;
    }

    /**
     * 修改奖项项目
     * @param awardproject 新的项目信息
     * @return 修改结果
     */
    @Transactional
    @Override
    public boolean updateAwardProject(Awardproject awardproject) {
        Award award = iAwardService.getOne(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getAwardID, awardproject.getAwardID()));
        checkRoleUtil.checkPermissionAchievement(award.getStatus());

        // 删除之前的项目
        removeById(awardproject.getAwardProjectID());

        // 新增当前项目
        addAwardProject(awardproject);

        return true;
    }

    /**
     * 根据奖项编号查询
     * @param awardID 奖项编号
     * @return 关联的项目列表
     */
    @Override
    public List<Awardproject> getAwardProjectByAwardID(String awardID) {
        Award byId = iAwardService.getById(awardID);
        if (Objects.isNull(byId)) {
            throw new BaseException("输入的奖项编号有误!");
        }
        List<Awardproject> awardprojects = baseMapper.selectList(Wrappers.lambdaQuery(Awardproject.class)
                .eq(Awardproject::getAwardID, awardID));

        // 将head字段中的用户ID转换为用户名
        for (Awardproject awardproject : awardprojects) {
            String head = awardproject.getHead();
            User user = iUserService.getById(head);
            awardproject.setHead(user.getName());
        }

        return awardprojects;
    }
}
